package com.jjunstar.googlemap;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class ServiceHandler {

	public static ServiceHandler instance;
	static String response = null;
	public final static int GET = 1;
	public final static int POST = 2;

	private ServiceHandler() {
	}

	/*
	 * Making service call
	 * @url - url to make request
	 * @method - http request method
	 * */
	
	public static ServiceHandler getInstance(){
		if(instance == null)
			instance = new ServiceHandler();
		
		return instance;
	}
	
	public String makeServiceCall(String url, int method) {
		return this.makeServiceCall(url, method, null);
	}

	/*
	 * Making service call
	 * @url - url to make request
	 * @method - http request method
	 * @params - http request params
	 * */
//	public String makeServiceCall(String url, int method,
//			List<NameValuePair> params) {
//		
//		StringBuilder dataStr = new StringBuilder();
//		try {
//			URL add_url = new URL(url);
//			HttpURLConnection conn = (HttpURLConnection)add_url.openConnection();
//			if(conn != null)
//			{
//				conn.setReadTimeout(10000);
//				conn.setUseCaches(false);
//				conn.setRequestMethod("GET");
//				conn.setRequestProperty("Accept-Charset", "UTF-8");
//				if(conn.getResponseCode() == HttpURLConnection.HTTP_OK)
//				{
//					BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
//					for(;;)
//					{
//						String line = br.readLine();
//						if(line == null) break;
//						dataStr.append(line);
//					}
//					br.close();
//					response = dataStr.toString();
//				}
//				conn.disconnect();
//				
//			}
//
//		} catch (UnsupportedEncodingException e) {
//			e.printStackTrace();
//		} catch (ClientProtocolException e) {
//			e.printStackTrace();
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//		
//		return response;
//
//	}
	
	public String makeServiceCall(String url, int method,
			List<NameValuePair> params) {
		try {
			// http client
			DefaultHttpClient httpClient = new DefaultHttpClient();
			HttpEntity httpEntity = null;
			HttpResponse httpResponse = null;

			// Checking http request method type
			if (method == POST) {
				HttpPost httpPost = new HttpPost(url);
				// adding post params
				if (params != null) {
					httpPost.setEntity(new UrlEncodedFormEntity(params));
				}

				httpResponse = httpClient.execute(httpPost);

			} else if (method == GET) {
				// appending params to url
				if (params != null) {
					String paramString = URLEncodedUtils
							.format(params, "utf-8");
					url += "?" + paramString;
				}
				HttpGet httpGet = new HttpGet(url);

				httpResponse = httpClient.execute(httpGet);

			}
			httpEntity = httpResponse.getEntity();
			response = EntityUtils.toString(httpEntity);

		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return response;

	}
	
}



